package com.coincalf.admin.web.config;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.coincalf.admin.annotation.Log;
import com.coincalf.admin.common.CommonUtils;
import com.coincalf.admin.common.ResultMap;
import com.coincalf.admin.entity.CoinType;
import com.coincalf.admin.entity.ForexCoin;
import com.coincalf.admin.entity.Market;
import com.coincalf.admin.entity.TradeArea;
import com.coincalf.admin.enums.SysLogTypeEnum;
import com.coincalf.admin.service.ForexCoinService;
import com.coincalf.admin.service.MarketService;
import com.coincalf.admin.service.TradeAreaService;
import com.google.common.base.Strings;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * <p>
 * 创新交易币种 前端控制器
 * </p>
 *
 * @author Haliyo
 * @since 2018-05-21
 */
@RestController
@RequestMapping("/forexCoin")
@Api(value = "创新交易币种", description = "创新交易币种管理")
public class ForexCoinController {

    @Autowired
    private ForexCoinService forexCoinService;

    @Autowired
    private TradeAreaService tradeAreaService;
    @Autowired
    private MarketService marketService;

    @Log(value = "查询创新交易币种列表",type = SysLogTypeEnum.SELECT)
    @GetMapping
    @ApiImplicitParams({
            @ApiImplicitParam(name = "size", value = "每页显示条数，默认 10", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态:0 禁用 1 启用", required = false, dataType = "String", paramType = "query"),
    })
    @ApiOperation(authorizations = {@Authorization(value="Authorization")},value = "按条件分页查询币种列表" ,httpMethod = "GET")
    public Object selectPage(@ApiIgnore Page<ForexCoin> page,
                             String name,String status) {
        EntityWrapper<ForexCoin> ew = new EntityWrapper<>();
        if (!Strings.isNullOrEmpty(name)) {
            ew.like("name", name);
        }
        if (!Strings.isNullOrEmpty(status)) {
            ew.eq("status", status);
        }
        ew.orderBy("created",false);
        return ResultMap.getSuccessfulResult(forexCoinService.selectPage(page, ew));
    }

    @Log(value = "查询币种信息",type = SysLogTypeEnum.SELECT)
    @GetMapping("/{id}")
    @ApiOperation(authorizations = {@Authorization(value="Authorization")},value = "币种信息" ,httpMethod = "GET")
    public Object selectOne(@PathVariable String id) {
        return ResultMap.getSuccessfulResult(forexCoinService.selectById(id));
    }

    @Log(value = "查询所有创新交易币种列表",type = SysLogTypeEnum.SELECT)
    @GetMapping("/all")
    @ApiOperation(authorizations = {@Authorization(value="Authorization")},value = "所有币种列表" ,httpMethod = "GET")
    public Object selectAll(String status) {
        EntityWrapper<ForexCoin>  ew= new EntityWrapper<ForexCoin>();
        if(StringUtils.isNotBlank(status)){
            ew.eq("status",status);
        }
        return ResultMap.getSuccessfulResult(forexCoinService.selectList(ew));
    }

    @Log(value = "币种列表_创建交易市场新增/编辑专用",type = SysLogTypeEnum.SELECT)
    @GetMapping("/load")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tradeAreaId", value = "交易区域ID", required = true, dataType = "String", paramType = "query"),
    })
    @ApiOperation(authorizations = {@Authorization(value="Authorization")},value = "币种列表_创建交易市场新增/编辑专用" ,httpMethod = "GET")
    public Object loadByForex(String tradeAreaId,String status) {
        if (Strings.isNullOrEmpty(tradeAreaId)) {
            return ResultMap.getFailureResult("交易区域ID不能为空!");
        }
        EntityWrapper  ew=  new EntityWrapper<TradeArea>();
        ew.eq("id",tradeAreaId);
        if(StringUtils.isNotBlank(status)){
            ew.eq("status",status);
        }
        TradeArea tradeArea = tradeAreaService.selectOne(ew);
        if(null == tradeArea){
            return ResultMap.getFailureResult("未找到对应交易区域!");
        }
        EntityWrapper  ew1=  new EntityWrapper<ForexCoin>();
        if(StringUtils.isNotBlank(status)){
            ew.eq("status",status);
        }
        List<ForexCoin> coinList = forexCoinService.selectList(ew1);
        ForexCoin coin = new ForexCoin();
        coin.setId(tradeArea.getCoinId());
        coin.setName(tradeArea.getCoinName());
        coinList.add(coin);
        return coinList;
    }

    @Log(value = "新增创新币种",type = SysLogTypeEnum.INSERT)
    @PostMapping
    @ApiOperation(authorizations = {@Authorization(value="Authorization")},value = "新增币种" ,httpMethod = "POST")
    public Object insert(@RequestBody ForexCoin coin) {
        if(forexCoinService.insert(coin)){
            return ResultMap.getSuccessfulResult("操作成功!");
        }else{
            return ResultMap.getFailureResult("操作失败!");
        }
    }

    @Log(value = "修改币种",type = SysLogTypeEnum.UPDATE)
    @PostMapping
    @ApiOperation(authorizations = {@Authorization(value="Authorization")},value = "修改币种")
    @RequestMapping(method = RequestMethod.PUT)
    public Object update(@RequestBody ForexCoin coin) {
        if(forexCoinService.updateById(coin)){
            return ResultMap.getSuccessfulResult("操作成功!");
        }else{
            return ResultMap.getFailureResult("操作失败!");
        }
    }

    @Log(value = "币种状态设置",type = SysLogTypeEnum.UPDATE)
    @PostMapping
    @ApiOperation(authorizations = {@Authorization(value="Authorization")},value = "币种状态设置(status:1启用 0禁用)")
    @RequestMapping(path = "/setStatus",method = RequestMethod.POST)
    public Object setStatus(@RequestBody ForexCoin coin) {
        ForexCoin oriCoin = forexCoinService.selectById(coin.getId());
        if(null == oriCoin){
            return ResultMap.getFailureResult("该记录不存在!");
        }
        if(coin.getStatus() > 1
                || coin.getStatus() < 0
                || coin.getStatus() == null){
            return ResultMap.getFailureResult("输入参数有误!");
        }
        if(coin.getStatus().equals(oriCoin.getStatus())){
            return ResultMap.getFailureResult("当前状态不能执行此操作!");
        }
        if(coin.getStatus().equals(CommonUtils.STATUS_0)){
            Market market=marketService.selectOne(new EntityWrapper<Market>().eq("type",CommonUtils.MARKET_TYPE_2).eq("sell_coin_id",coin.getId()).or("buy_coin_id",coin.getId()));
            if(market!=null){
                return ResultMap.getFailureResult("币种配置"+coin.getId()+"已在使用，不能进行禁用！");
            }
        }

        ForexCoin newCoin = new ForexCoin();
        newCoin.setId(coin.getId());
        newCoin.setStatus(coin.getStatus());
        if(forexCoinService.updateById(newCoin)){
            return ResultMap.getSuccessfulResult("操作成功!");
        }else{
            return ResultMap.getSuccessfulResult("操作失败!");
        }
    }

}

